<?php
namespace system\core;

/**
 * Error Handler
 * 
 * The Error Handler handles all the exceptions in the application. To edit the
 * view of the error, edit "error_view.php" in the "views" folder.
 * 
 * @author Steffen Brem <steffenbrem@gmail.com>
 * @package System.core
 */
class Exception 
{
    
    /**
     * Stores the trace as an array
     * 
     * @var array
     */
    private static $_trace;
    
    /**
     * Stores the title of the error
     * 
     * Example: An Error Occurred
     * 
     * @var string
     */
    private static $_title;
    
    /**
     * Stores the filename where the error occurred
     * 
     * @var string
     */
    private static $_file;
    
    /**
     * Stores the message (the error itself)
     * 
     * @var string
     */
    private static $_message;
    
    /**
     * Stores the linenumber where the error occurred
     *  
     * @var string
     */
    private static $_line;
    
    /**
     * Stores the code of the error
     * 
     * @var string
     */
    private static $_code;
    
    /**
     * Stores the trace as a string
     * 
     * @var string
     */
    private static $_traceString;
    
    /**
     * Show an error message
     * 
     * Parameter must be an exception instance
     * 
     * @param Exception $e 
     */
    public static function show( \Exception $e )
    {
        self::$_trace       = $e->getTrace();
        self::$_file        = $e->getFile();
        self::$_message     = $e->getMessage();
        self::$_line        = $e->getLine();
        self::$_code        = $e->getCode();
        self::$_traceString = $e->getTraceAsString();
        
        switch ( self::$_code )
        {
            case '404':
                self::$_title = 'Page Not Found';
            break;
            default:
                self::$_title = 'An Error Occurred';
        }
        
        // Clean the output buffering
        ob_clean();
        
        require_once SYS_PATH . 'core/views/error_view.php';
        
        die();
    }
    
    /**
     * Get the Error title
     * 
     * @return string
     */
    public static function getTitle()
    {
        return self::$_title;
    }
    
    /**
     * Get the file name
     * 
     * @return string
     */
    public static function getFile()
    {
        if ( isset(self::$_trace['0']['file']) )
        {
            return self::$_trace['0']['file'];
        }
        else
        {
            return self::$_file;
        }
    }
    
    /**
     * Get the Exception message
     *  
     * @return string
     */
    public static function getMessage()
    {
        return self::$_message;
    }
    
    /**
     * Get the line number
     * 
     * @return string
     */
    public static function getLine()
    {
        if ( isset(self::$_trace['0']['line']) )
        {
            return self::$_trace['0']['line'];
        }
        else
        {
            return self::$_line;
        }
    }
    
    /**
     * Get the stacktrace as a string
     * 
     * @return string
     */
    public static function getTrace()
    {
        return nl2br(htmlentities(self::$_traceString));
    }
    
    public static function getAppName()
    {
        return ( ! Config::get('config', 'app_name') ) ? 'MyApp' : Config::get('config', 'app_name');
    }
    
    public static function baseUrl()
    {
        require_once SYS_PATH . '/helpers/url_helper.php';
        
        return base_url();
    }
}

/* End of file ErrorHandler.php */
